<?php
namespace app\index\controller;

class Upload extends Base
{
    /**
     * 单文件上传
     * @param $files
     * @param $path
     * @return array
     */
    public function aloneUpload(){
        $file = request()->file('file');
        if (!$file){
            $res = ["code"=> 2
                ,"msg"=> '请上传文件'
                ,"data"=> [],
            ];
        }else{
            // 移动到框架应用根目录/public/uploads/ 目录下
            $get_info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($get_info){
                $info['getSaveName'] =  'uploads/'.$get_info->getSaveName();
                $res = ["code"=> 1
                    ,"msg"=> ""
                    ,"data"=> [
                        "src"=> $info['getSaveName']
                    ],
                ];

            }else{
                // 上传失败获取错误信息
                $res = ["code"=> 0
                    ,"msg"=> $file->getError()
                    ,"data"=> [
                        "src"=> "http://cdn.layui.com/123.jpg"
                    ],
                ];
            }
        }

        return json_encode($res);
    }
}